<template>
  <el-dialog
    title="添加设备"
    :visible.sync="open"
    :before-close="cancel"
    width="600px"
    append-to-body
  >
    <el-form ref="form" :model="form" :rules="rules" class="dialog-form">
      <el-form-item label="验证码" prop="code">
        <el-input v-model="form.code" placeholder="请输入验证码" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm" :loading="loading">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addDevice } from "@/api/login";
export default {
  props: {
    open: {
      type: Boolean,
      default: false,
    },
    roleId: {
      type: [String, Number],
      default: "",
    },
  },
  data() {
    return {
      form: {
        code: null,
      },
      rules: {
        code: [{ required: true, trigger: "blur", message: "验证码不能为空" }],
      },
      loading: false,
    };
  },
  methods: {
    cancel() {
      this.$emit("cancel");
      this.resetFormFun();
    },
    resetFormFun() {
      this.form = {
        code: null,
      };
      this.resetForm("form");
    },
    async submitForm() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          try {
            const {
              form: { code },
              roleId,
            } = this;
            this.loading = true;
            await addDevice(code, roleId);
            this.$modal.msgSuccess("添加成功");
            this.cancel();
            this.$emit("getList");
          } catch (error) {
            console.error("添加错误", error);
          } finally {
            this.loading = false;
          }
        }
      });
    },
  },
};
</script>

<style scoped lang="scss">
.intell-slider {
  display: flex;
  .slider {
    max-width: 300px;
    width: 100%;
  }
}
.intell-txt {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
::v-deep .block-label label {
  display: block;
  float: none;
  text-align: left;
}
.dialog-form .el-select {
  width: 100%;
}
</style>
<style>
.intel-collapse,
.intel-collapse .el-collapse-item__header {
  border-bottom: none;
  border-top: none;
}
.intel-collapse .el-collapse-item__header {
  display: flex;
  flex-direction: row-reverse;
  justify-content: flex-end;
}
.intel-collapse .el-collapse-item__arrow {
  margin: 0 10px;
}
.intell-slide-item label {
  float: none;
}
</style>
